//
//  SuggestController.m
//  UI Demo
//
//  Created by Rainy Zou on 15/11/30.
//  Copyright © 2015年 Rainy Zou. All rights reserved.
//

#import "SuggestController.h"
#import "SuggestGroup.h"
#import "SuggestCell.h"
#import "SuggestHeaderView.h"

@interface SuggestController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong)NSMutableArray *array;



@end

@implementation SuggestController

static NSString *reuserId = @"pCell";
static NSString *headerReuseId = @"header";

-(NSMutableArray *)array
{
    if(_array == nil)
    {
        _array = [NSMutableArray array];
        NSArray *temp = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Suggest" ofType:@"plist" ]];
        for (NSDictionary *dict in temp) {
            SuggestGroup *group = [SuggestGroup groupByDict:dict];
            NSArray *temp2 = group.suggests;
            NSMutableArray *muSuggest = [NSMutableArray array];
            for (NSDictionary *dict2 in temp2) {
                SuggestRd *suggest = [SuggestRd suggestRdByDict:dict2];
                [muSuggest addObject:suggest];
            }
            group.suggests = muSuggest;
            [_array addObject:group];
        }
    }
    return _array;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    if([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        SuggestGroup *group = self.array[indexPath.section];
        SuggestHeaderView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerReuseId forIndexPath:indexPath];
       // view.button.titleLabel.text =group.name;
        [view.button setTitle:group.name forState:UIControlStateNormal];
        return view;
    }
    return nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flow];
    [collectionView registerClass:[SuggestCell class] forCellWithReuseIdentifier:reuserId];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    [collectionView registerClass:[SuggestHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerReuseId];
    
    [self.view addSubview:collectionView];
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 50);
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    SuggestGroup *group = self.array[section];
    return group.suggests.count;
    
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.array.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SuggestCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuserId forIndexPath:indexPath];
    
    SuggestGroup *group = self.array[indexPath.section];
    cell.suggest = group.suggests[indexPath.row];
    
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(80, 80);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}


@end
